/*
 * Copyright (c) Jan Šlahora (${USER}) - 2012.
 * Created in IntelliJ IDEA 10.5
 */

/*
 * Created by JFormDesigner on Tue Mar 20 12:43:58 CET 2012
 */

package GUIComponents.AgregaceComponents;

import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.TableColumnModel;

import com.intellij.uiDesigner.core.*;
import com.jgoodies.forms.factories.*;
import com.jgoodies.forms.layout.*;
import statistics.Agregace;
import statistics.Pocitani;

/**
 * @author Karel Novak
 */
public class AgregacePanel extends JPanel {
  public AgregacePanel(Agregace a) {
    initComponents();
    JTable t = dejTabulku(a);

    panelTabulka.add(t, CC.xy(1, 1));
    int vyskaTabulky = t.getRowHeight() * t.getRowCount();
    splitPaneTabulkaGraf.setDividerLocation(vyskaTabulky);

    labelD.setText("D = " + a.getKoeficientD());

    Dimension d = panelGraf.getSize();
    AgregaceGraf graf = new AgregaceGraf(a.getPrumerySi(), a.getTeoretickeSi(), labelHodnota, t);
    panelGraf.add(graf, CC.xy(1, 1));
  }

  public JLabel getLabelHodnota() {
    return labelHodnota;
  }

  private void initComponents() {
    // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
    // Generated using JFormDesigner Evaluation license - Karel Novak
    splitPaneTabulkaGraf = new JSplitPane();
    panelTabulka = new JPanel();
    panel1 = new JPanel();
    panelGraf = new JPanel();
    panelStatusBar = new JPanel();
    labelD = new JLabel();
    labelHodnota = new JLabel();

    //======== this ========

    // JFormDesigner evaluation mark
    setBorder(new javax.swing.border.CompoundBorder()); addPropertyChangeListener(new java.beans.PropertyChangeListener(){public void propertyChange(java.beans.PropertyChangeEvent e){if("border".equals(e.getPropertyName()))throw new RuntimeException();}});

    setLayout(new FormLayout(
      "123dlu:grow",
      "fill:94dlu:grow"));

    //======== splitPaneTabulkaGraf ========
    {
      splitPaneTabulkaGraf.setOrientation(JSplitPane.VERTICAL_SPLIT);
      splitPaneTabulkaGraf.setEnabled(false);

      //======== panelTabulka ========
      {
        panelTabulka.setLayout(new FormLayout(
          "default:grow",
          "fill:default:grow"));
      }
      splitPaneTabulkaGraf.setTopComponent(panelTabulka);

      //======== panel1 ========
      {
        panel1.setLayout(new FormLayout(
          "default:grow",
          "167dlu:grow, 18dlu"));

        //======== panelGraf ========
        {
          panelGraf.setLayout(new FormLayout(
            "default:grow",
            "fill:163dlu:grow"));
        }
        panel1.add(panelGraf, CC.xy(1, 1, CC.FILL, CC.FILL));

        //======== panelStatusBar ========
        {
          panelStatusBar.setBorder(new EmptyBorder(1, 0, 0, 0));
          panelStatusBar.setLayout(new FormLayout(
            "127dlu, default:grow",
            "fill:default:grow"));

          //---- labelD ----
          labelD.setHorizontalAlignment(SwingConstants.LEFT);
          labelD.setBorder(new MatteBorder(1, 0, 0, 0, Color.gray));
          labelD.setVerticalAlignment(SwingConstants.BOTTOM);
          labelD.setVerticalTextPosition(SwingConstants.BOTTOM);
          panelStatusBar.add(labelD, CC.xywh(1, 1, 2, 1, CC.DEFAULT, CC.FILL));

          //---- labelHodnota ----
          labelHodnota.setHorizontalAlignment(SwingConstants.RIGHT);
          labelHodnota.setVerticalTextPosition(SwingConstants.BOTTOM);
          labelHodnota.setVerticalAlignment(SwingConstants.BOTTOM);
          panelStatusBar.add(labelHodnota, CC.xy(2, 1));
        }
        panel1.add(panelStatusBar, CC.xy(1, 2, CC.DEFAULT, CC.FILL));
      }
      splitPaneTabulkaGraf.setBottomComponent(panel1);
    }
    add(splitPaneTabulkaGraf, CC.xy(1, 1));
    // JFormDesigner - End of component initialization  //GEN-END:initComponents
  }

  private JTable dejTabulku(Agregace a) {
    JTable t = new JTable(4, a.getPrumerySi().size()+1);
    t.setValueAt("i", 0, 0);
    t.setValueAt("Li", 1, 0);
    t.setValueAt("∅ S", 2, 0);
    t.setValueAt("Ŝ",3, 0);
    for (int sloupce = 0; sloupce < a.getPrumerySi().size(); sloupce++) {
      t.setValueAt(sloupce + 1, 0, sloupce+1);
      t.setValueAt(sloupce + 1, 1, sloupce + 1);
      double d = a.getPrumerySi().get(sloupce);
      t.setValueAt(Pocitani.zaokrouhleni(d, 2), 2, sloupce + 1);
      d = a.getTeoretickeSi().get(sloupce);
      t.setValueAt(Pocitani.zaokrouhleni(d, 2), 3, sloupce + 1);
    }

    t.setTableHeader(null);
    t.setRowSelectionAllowed(false);
    t.setColumnSelectionAllowed(true);
    return t;
  }

  // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
  // Generated using JFormDesigner Evaluation license - Karel Novak
  private JSplitPane splitPaneTabulkaGraf;
  private JPanel panelTabulka;
  private JPanel panel1;
  private JPanel panelGraf;
  private JPanel panelStatusBar;
  private JLabel labelD;
  private JLabel labelHodnota;
  // JFormDesigner - End of variables declaration  //GEN-END:variables
}
